<?php
/**
 * Created by PhpStorm.
 * User: xionghao
 * Date: 2017/10/16
 * Time: 17:51
 */

namespace app\volunteer\controller;

use app\volunteer\model\VolunteerLogModel;
use app\volunteer\model\VolunteerModel;
use cmf\controller\HomeBaseController;
use think\Controller;
use think\helper\Hash;

class IndexController extends HomeBaseController
{
    public function ok()
    {
        return $this->fetch(':success');
    }

    public function index()
    {
        if (session('volunteer_login')) {
            $volunteer = session('volunteer');
            $volunteer = VolunteerModel::get($volunteer['id']);
            $this->assign('volunteer', $volunteer);
            session('volunteer', $volunteer);
            return $this->fetch(":index");
        } else {
            $this->redirect('volunteer/login/index');
        }
    }

    public function logs()
    {
        if (session('volunteer_login')) {
            $volunteer = session('volunteer');
            $volunteer = VolunteerModel::get($volunteer['id']);
            $this->assign('volunteer', $volunteer);
            if ($volunteer['status'] != 1) {
                $this->redirect("index");
            }
            $logModel = new VolunteerLogModel();
            $logs = $logModel->where('vid', $volunteer['id'])->select();
            $this->assign('logs', $logs);
            return $this->fetch(':logs');
        } else {
            $this->redirect('volunteer/login/index');
        }
    }

    public function add()
    {
        return $this->fetch(':add');
    }

    public function delete()
    {
        $id = $this->request->param('id');
        $volunteer = session('volunteer');
        if ($volunteer['status'] != 1) {
            $this->error('没有权限');
        }
        $volunteerLogModel = new VolunteerLogModel();
        $result = $volunteerLogModel->where('id', intval($id))->where('vid', intval($volunteer['id']))->find();
        if ($result) {
            $result->delete();
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    public function edit()
    {
        $id = $this->request->param('id');

        $volunteer = session('volunteer');
        if ($volunteer['status'] != 1) {
            $this->error('没有权限');
        }
        $volunteerLogModel = new VolunteerLogModel();
        $result = $volunteerLogModel->where('id', intval($id))->where('vid', intval($volunteer['id']))->find();
        $this->assign('log',$result);
        return $this->fetch(':edit');
    }

    public function editPost()
    {
        $data = $this->request->param();
        $volunteer = session('volunteer');
        if ($volunteer['status'] != 1) {
            $this->error('没有权限');
        }
        $volunteerLogModel =  VolunteerLogModel::get($data['id']);

        $volunteerLogModel->day = $data['day'];
        $volunteerLogModel->time = $data['time'];
        $volunteerLogModel->address = $data['address'];
        $volunteerLogModel->content = $data['content'];
        $volunteerLogModel->beizhu = $data['beizhu'];
        $volunteerLogModel->save();

        $this->success('添加成功');
    }

    public function addPost()
    {
        $data = $this->request->param();
        $volunteer = session('volunteer');
        if ($volunteer['status'] != 1) {
            $this->error('没有权限');
        }

        $volunteerLogModel = new VolunteerLogModel();
        $volunteerLogModel->vid = $volunteer['id'];
        $volunteerLogModel->day = $data['day'];
        $volunteerLogModel->time = $data['time'];
        $volunteerLogModel->address = $data['address'];
        $volunteerLogModel->content = $data['content'];
        $volunteerLogModel->beizhu = $data['beizhu'];
        $volunteerLogModel->save();

        $this->success('添加成功');
    }

    public function logout()
    {
        session('volunteer_login', 0);
        session('volunteer', null);
        $this->success('退出登录成功');
    }

}